/* Copyright (C) 2013 Dr2co
 *
 * Created with IntelliJ IDEA.
 * User: Dr2co
 * Date: 01.07.13
 *
 *  pt-emu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  pt-emu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with pt-emu.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aionengine.gameserver.network.loginserver.clientpackets;

import com.aionengine.gameserver.model.account.AccountMembership;
import com.aionengine.gameserver.model.account.AccountTime;
import com.aionengine.gameserver.model.account.AccountToll;
import com.aionengine.gameserver.network.loginserver.LoginServer;
import com.aionengine.gameserver.network.loginserver.LsClientPacket;

/**
 * In this packet LoginServer is answering on GameServer request about valid authentication data and also sends account
 * name of user that is authenticating on GameServer.
 *
 * @author -Nemesiss-, Dr2co
 */
public class CM_ACOUNT_AUTH_RESPONSE extends LsClientPacket {

    public CM_ACOUNT_AUTH_RESPONSE(int opCode) {
        super(opCode);
    }

    /**
     * accountId
     */
    private int accountId;

    /**
     * result - true = authed
     */
    private boolean result;

    /**
     * accountName [if response is ok]
     */
    private String accountName;
    /**
     * accountTime
     */
    private AccountTime accountTime;
    /**
     * access level - regular/gm/admin
     */
    private byte accessLevel;
    /**
     * Membership - regular/premium
     */
    private AccountMembership membership;

    /**
     * Toll
     */
    private AccountToll toll;

    /**
     * {@inheritDoc}
     */
    @Override
    public void readImpl() {
        accountId = readD();
        result = readC() == 1;

        if (result) {
            accountName = readS();
            accountTime = new AccountTime();

            accountTime.setAccumulatedOnlineTime(readQ());
            accountTime.setAccumulatedRestTime(readQ());

            accessLevel = (byte) readC();
            membership = new AccountMembership();

            membership.setMembership((byte) readC());
            membership.setMembershipExpire(readQ());
            membership.setCraftship((byte) readC());
            membership.setCraftshipExpire(readQ());
            membership.setApship((byte) readC());
            membership.setApshipExpire(readQ());
            membership.setCollectionship((byte) readC());
            membership.setCollectionshipExpire(readQ());

            toll = new AccountToll();

            toll.setToll(readQ());
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void runImpl() {
        LoginServer.getInstance().accountAuthenticationResponse(accountId, accountName, result, accountTime, accessLevel, membership, toll);
    }
}
